//
//  FBDetailViewController.m
//  FindBook
//
//  Created by tianke on 13-11-2.
//  Copyright (c) 2013年 lzx. All rights reserved.
//

#import "FBDetailViewController.h"
#import "RYCHTTPImageView.h"

@interface FBDetailViewController ()

@end

@implementation FBDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = _bookData.title;
    [self loadCommpentsView];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadCommpentsView
{
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_grayBg.png"]];
    bgImageView.frame = self.view.bounds;
    [self.view addSubview:bgImageView];
    
    UIScrollView *bgScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    bgScrollView.backgroundColor = [UIColor clearColor];
    if (IOS_VERSION >= 7.0) {
        bgScrollView.frame = CGRectMake(0, 64, 320, 416);
    }
    [self.view addSubview:bgScrollView];
    
    CGFloat height = 10;
    
    RYCHTTPImageView *imageView = [[RYCHTTPImageView alloc] initWithFrame:CGRectMake(54, height, 204, 271)];
    [imageView setURLImage:[NSURL URLWithString:_bookData.img] imageQueue:self.httpImageQueue];
    [bgScrollView addSubview:imageView];
    
    height += 271 + 10;
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(52, height, 208, 1)];
    lineLabel.backgroundColor = [UIColor blackColor];
    [bgScrollView addSubview:lineLabel];
    
    CGFloat marginLeft = 10;
    CGFloat labelWidth = 320 - marginLeft * 2;
    
    height += 1 + 20;
    UIFont *titleFont = [UIFont boldSystemFontOfSize:16];
    CGSize titleSize = [self stringSize:_bookData.title font:titleFont];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(marginLeft, height, labelWidth, titleSize.height)];
    titleLabel.font = titleFont;
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    titleLabel.text = _bookData.title;
    [bgScrollView addSubview:titleLabel];
    
    height += titleSize.height + 10;
    UIFont *subTitleFont = [UIFont boldSystemFontOfSize:14];
    CGSize subTitleSize = [self stringSize:_bookData.sub1 font:subTitleFont];
    
    UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(marginLeft, height, labelWidth, subTitleSize.height)];
    subTitleLabel.font = subTitleFont;
    subTitleLabel.numberOfLines = 0;
    subTitleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    subTitleLabel.text = _bookData.sub1;
    [bgScrollView addSubview:subTitleLabel];
    
    height += subTitleSize.height + 10;
    UIFont *detailFont = [UIFont systemFontOfSize:14];
    CGSize detailSize = [self stringSize:_bookData.sub2 font:detailFont];
    
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(marginLeft, height, labelWidth, detailSize.height)];
    detailLabel.font = detailFont;
    detailLabel.lineBreakMode = NSLineBreakByCharWrapping;
    detailLabel.numberOfLines = 0;
    detailLabel.text = _bookData.sub2;
    [bgScrollView addSubview:detailLabel];
    
    height += detailSize.height + 15;
    bgScrollView.contentSize = CGSizeMake(320, height);
}

- (CGSize)stringSize:(NSString *)string font:(UIFont *)font
{
    CGFloat maxHeight = 10000;
    CGSize size = [string sizeWithFont:font constrainedToSize:CGSizeMake(320, maxHeight) lineBreakMode:NSLineBreakByWordWrapping];
    return size;
}

@end
